﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class msg
    {
        public static dynamic getMsg(int mid, string username, bool god)
        {
            var sql = new Sql.SqlDataContext();
            var m = sql.Message.FirstOrDefault(row => row.mid == mid);
            if (m == null) return new
                {
                    success = false,
                    msg = "消息不存在"
                };
            if (!god && m.to != username) return new
            {
                success = false,
                msg = "拒绝访问"
            };
            var read = m.read;
            if (!read)
            {
                m.read = true;
                sql.SubmitChanges();
            }
            var u = sql.User.First(row => row.username == m.from);
            return new
            {
                success = true,
                m.mid,
                m.title,
                username = m.from,
                u.nickname,
                time = m.time.toTimeString(),
                m.content,
                read
            };
        }
    }
}